package cn.tedu._05mvcboot03.controller;

import cn.tedu._05mvcboot03.base.response.JsonResult;
import cn.tedu._05mvcboot03.base.response.StatusCode;
import cn.tedu._05mvcboot03.mapper.ArticleMapper;
import cn.tedu._05mvcboot03.pojo.dto.UserArticleDTO;
import cn.tedu._05mvcboot03.pojo.entity.Article;
import cn.tedu._05mvcboot03.pojo.vo.ArticleVO;
import cn.tedu._05mvcboot03.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.Size;

@Api(tags ="文章发布")
@Slf4j
@RestController
@RequestMapping("/v1/users/")
@Validated
public class ArticleController {
    @Autowired
    private ArticleMapper articleMapper;
    @PostMapping("article")
    @ApiOperation(value = "文章发布")
    public JsonResult insert(@RequestBody  @Validated UserArticleDTO userArticleDTO, @ApiIgnore HttpSession session){
        UserVO userVO = (UserVO)session.getAttribute("user");
//        if (userVO==null){
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }拦截器拦截
        Article article = new Article();
        BeanUtils.copyProperties(userArticleDTO,article);
        article.setUserId(userVO.getId());
        articleMapper.insert(article);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }
    @PostMapping("selectByArticleId")
    @ApiOperation(value = "文章查询")
    public JsonResult selectByArticleId(@Range(min = 100,max = 200,message = "文章id必须为100-200之间") @RequestParam Long id){
        ArticleVO articleVO = articleMapper.selectById(id);
        log.debug("articleVO="+articleVO);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,articleVO);
    }
}
